package com.ucode.hrm.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ucode.hrm.mapper.HrmCustomerProjectMapper;
import com.ucode.model.hrm.HrmCustomerProject;
import com.ucode.model.hrm.enums.HrmSysResultCodeEnum;
import com.ucode.springboot.starter.mybatis.core.base.BaseService;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户项目 Service
 * @Author: liliang
 * @Date: 2025/6/1 20:19
 * @Version: 1.0
 */
public interface HrmCustomerProjectService extends BaseService<Long, HrmCustomerProject, HrmCustomerProjectMapper> {

    /**
     * 变更状态
     * @param ids
     * @param status
     * @return
     */
    public int updateStatus(Collection<Long> ids , Integer status);

    default HrmCustomerProject queryOneByName(Long customerCompanyId, String projectName){
        if(ObjUtil.isNull(customerCompanyId) || StrUtil.isBlank( projectName))
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);
        Map<String,Object> condition = new HashMap<>();
        condition.put("customerCompanyId",customerCompanyId);
        condition.put("projectName",projectName);
        condition.put("deltag",false);
        List<HrmCustomerProject> customerCompanys = queryObjectForList(condition,1);
        return CollUtil.isEmpty(customerCompanys) ? null: customerCompanys.get(0);
    }

    default Map<Long, HrmCustomerProject> getMap(Collection<Long> ids) {
        return CollUtil.isEmpty(ids) ? new HashMap() : CollectionUtils.convertMap(this.selectByPrimaryKeys(ids), HrmCustomerProject::getId);
    }

    /**
     * 验证是否合法并返回
     * @param id
     * @return
     */
    default HrmCustomerProject validate(Long id){
        if(ObjectUtil.isNull(id) )
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);
        //验证数据是否存在
        HrmCustomerProject brand = this.selectByPrimaryKey(id);
        if(ObjectUtil.isNull(brand) || brand.getDeltag()){
            throw new UcodeServiceException(HrmSysResultCodeEnum.CUSTOMER_PROJECT_NOT_EXISTS);
        }
        return brand;
    }

}
